
var findStuInfo = toContextPath('/teacher/teacher_getInfo.action');
var findHomework = toContextPath('/teacher/homework_getAll.action');
var findHwItem = toContextPath('/teacher/homework_getById.action');
var loginHtml = toContextPath('/TeacherPages/login.html');
var findeTest = toContextPath('/teacher')

var homeworkState;
$(function(){	
	initPage();
});


function initPage() {
//	$('#hwContentDiv').hide();
	location.hash = '';
	$.getJSON(findStuInfo, function(data) {
		if (data.result && data.result.teaName){
			$('#teaName').text(data.result.teaName);
		} else {
			window.location.href = loginHtml;
		}
	});
	$.getJSON(findHomework, function(data) {
		if (data.result && data.result.length > 0){
			$.each(data.result, function(index, homework) {
				var item = '<tr ';
				if (homework.state == 0) {
					item += 'class="warning">';
					homeworkState = "Todo";
				} else if (homework.state == 1) {
					item += 'class="success">';
					homeworkState = "Done";
				}
				item = item + '<td>' + homework.hwCreateDate + '</td>'
				+ '<td>' + homework.hwDeadline + '</td>'
				+ '<td> <a onclick="clickA('+ homework.hwId+ ')">' + homework.hwName + '</a></td>'
				+ '<td>' + homework.teaName + '</td>'
				+ '<td ';
				if (homework.state == 0)
					item += 'style="color:red">' + homeworkState + '</td>'+ '</tr>';
				else if (homework.state == 1)
					item += 'style="color:green">' + homeworkState + '</td>'+ '</tr>';
				
				$('#homeworkList').append(item);
			});			
		}
	});
	
	$('#hwCreateDate').val(function() { 
		var currentDate = new Date();
        var year = currentDate.getFullYear();
        var month = currentDate.getMonth()+1; 
        var day = currentDate.getDate();
        var hour = currentDate.getHours();
        var minute = currentDate.getMinutes();
        var second = currentDate.getSeconds();
        var str = year + "-";
        month>9?(str += month):(str += "0" + month);
        str += "-";
        day>9?(str += day):(str += "0" + day);
        str += " ";
        hour>9?(str += hour):(str += "0" + hour);
        str += ":";
        minute>9?(str += minute):(str += "0" + minute);
        str += ":";
        second>9?(str += second):(str += "0" + second);
        return   str;   
    });
	
	$('#hwDeadline').val(function() { 
		var currentDate = new Date();
        var year = currentDate.getFullYear();
        var month = currentDate.getMonth()+1; 
        var day = currentDate.getDate();
        var str = year + "-";
        month>9?(str += month):(str += "0" + month);
        str += "-";
        day>9?(str += day):(str += "0" + day);
        str += " 23:59:59";
       
        return   str;   
    });
}
function clickA(hwId) {
		$.getJSON(findHwItem, {hwId: hwId}, function(hwItem) {
			if (hwItem.success) {
				$('#hwContentTxt').text(hwItem.result.hwContent);
			//	$('#hwContentDiv').toggle();
			}
			
		});
}

//$("#userPwd").formValidator();